package com.zmn.oms.admin.controller.work.modify;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.engstock.common.model.dio.RoutePlanDIO;
import com.zmn.engstock.common.model.dro.RoutePlanDRO;
import com.zmn.engstock.common.model.dro.StockItemDRO;
import com.zmn.engstock.common.model.dro.StockUsedDRO;
import com.zmn.engstock.dubbo.interfaces.stock.StockListRemoteService;
import com.zmn.oms.admin.controller.work.WorkBaseController;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.product.OrderProduct;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.vo.orderstock.MasterStockVO;
import com.zmn.oms.services.interfaces.product.OrderProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @ClassName WorkMasterStockController
 * @description:
 * @author: wangyanci
 * @create: 2021-11-09 16:29
 * @Version 1.0
 **/
@Slf4j
@Controller
@RequestMapping("/work/modify")
public class WorkMasterStockController extends WorkBaseController {

    @Autowired
    private OrderProductService orderProductService;

    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    private StockListRemoteService stockListRemoteService;

    /**
     * 检查派单
     */
    @RequestMapping(value = "/master/stock")
    @ResponseBody
    public ResultDTO checkDistributeReason(Long orderId, Long workId, Integer masterId) throws OmsBaseException {

        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, workId);
        if (Objects.isNull(orderWork)) {
            throw new OmsBaseException("工单不存在！");
        }
        List<OrderProduct> orderProductList = orderProductService.listOrderProductByOrderId(orderId);
        if (CollectionUtil.isNullOrEmpty(orderProductList)) {
            throw new OmsBaseException("产品信息不存在！");
        }
        String dutyTime = DateUtil.toString(Objects.isNull(orderWork.getDutyTime()) ? DateUtil.getNow() : orderWork.getDutyTime(), DateUtil.FORMAT_DATE);
        RoutePlanDIO routePlanDIO = new RoutePlanDIO();
        routePlanDIO.setEngineerId(masterId);
        routePlanDIO.setProductGroupId(orderWork.getServProductGroupId());
        routePlanDIO.setDate(dutyTime);
        routePlanDIO.setProductNum(orderProductList.get(0).getNumber());
        ResponseDTO<RoutePlanDRO> responseDTO = stockListRemoteService.getRoutePlanInfoByEngineerAndProduct(routePlanDIO);
        log.info("行程计划-remote getRoutePlanInfoByEngineerAndProduct----->入参：{}，出参:{}", JSON.toJSONString(routePlanDIO), JSON.toJSONString(responseDTO));
        if (!responseDTO.isSuccess()) {
            throw new OmsBaseException(responseDTO.getMessage());
        }
        // 处理排序时间
        RoutePlanDRO data = responseDTO.getData();
        List<StockItemDRO> stockItemDROList = data.getStockItemDROList();
        List<StockUsedDRO> stockUsedDROList = data.getStockUsedDROList();

        List<MasterStockVO> masterStockVOS = Lists.newArrayList();
        for (StockItemDRO dro : stockItemDROList) {
            MasterStockVO masterStockVO = new MasterStockVO();
            masterStockVO.setStartTime(dro.getStartTime());
            masterStockVO.setEndTime(dro.getEndTime());
            masterStockVO.setIsTakeUp(GlobalConsts.NO);
            masterStockVO.setSortTime(this.processSortTime(dutyTime, dro.getStartTime()));
            masterStockVOS.add(masterStockVO);
        }
        for (StockUsedDRO dro : stockUsedDROList) {
            MasterStockVO masterStockVO = new MasterStockVO();
            masterStockVO.setStartTime(dro.getStartTime());
            masterStockVO.setEndTime(dro.getEndTime());
            masterStockVO.setIsTakeUp(GlobalConsts.YES);
            masterStockVO.setSortTime(this.processSortTime(dutyTime, dro.getStartTime()));
            masterStockVOS.add(masterStockVO);
        }
        masterStockVOS.sort(Comparator.comparing(MasterStockVO::getSortTime));
        return ResultDTO.success(masterStockVOS);
    }


    private Date processSortTime(String dutyTime, String startTime) {
        String[] split = startTime.split(":");
        String hours = split[0];
        String minutes = split[1];
        int min = Integer.parseInt(hours) * 60 + Integer.parseInt(minutes);
        return DateUtil.addMinute(DateUtil.parse(dutyTime, DateUtil.FORMAT_DATE), min);
    }

}